import { AfterViewInit, Component, Injector, ViewEncapsulation, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { EntityDtoOfInt64, UserListDto, WorkflowServiceProxy, UserServiceProxy, GetWorkflowInstanceViewDto, OutsourcingWorkFlowServiceProxy, WorkflowStatus, WorkflowInstanceDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
import * as moment from 'moment';
import { from } from 'rxjs';
import { stat } from 'fs';
import { FlowworkauditDetailModelComponent } from '../flowworkauditDetailModel/flowworkauditDetailModel.component';

@Component({
    selector: 'app-myapply',
    templateUrl: './myapply.component.html',
    styleUrls: ['./myapply.component.css']
})

export class MyapplyComponent extends AppComponentBase {
    @ViewChild('flowworkauditDetailModel') flowworkauditDetailModel: FlowworkauditDetailModelComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    filterText = '';
    getWorkflowInstanceViewDtos: GetWorkflowInstanceViewDto[];

    constructor(injector: Injector,
        private _workflowServiceProxy: WorkflowServiceProxy,
        private _outsourcingWorkFlowServiceProxy: OutsourcingWorkFlowServiceProxy,
        private _userServiceProxy: UserServiceProxy) {
        super(injector);
    }
    getMyApplyWorkflow(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.primengTableHelper.showLoadingIndicator();
        this._workflowServiceProxy.getWorkflowIdsOfOwnProssessor(
            "",
            "",
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event),
            null
        )
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator()))
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                //this.primengTableHelper.records = result.items;
                this.getWorkflowInstanceViewDtos = result.items;
                this.primengTableHelper.hideLoadingIndicator();
                console.log(result.items)
                _.forEach(this.getWorkflowInstanceViewDtos, viewItem => {
                    // if ( viewItem.poccessorId !== '') {
                    //   this._userServiceProxy.getUserForAll(viewItem.poccessorId)
                    //   .subscribe(userResult => {
                    //       console.log(userResult);
                    //       let currentD = this.getWorkflowInstanceViewDtos.find((element) => (element.id === viewItem.id));
                    //       if(userResult.allOrganizationUnits.length>0)
                    //       {
                    //         currentD.poccessorOuName = userResult.allOrganizationUnits[0].displayName;
                    //       }
                    //       currentD.poccessorName =  userResult.user.name;
                    //   });
                    // } else {
                    //   let currentD = this.getWorkflowInstanceViewDtos.find((element) => (element.id === viewItem.id));
                    //   currentD.poccessorName =  '-';
                    //   currentD.poccessorOuName = '-';
                    // }
                    // this._workflowServiceProxy.getExecutionPointer(viewItem.id)
                    // .subscribe(pointResult => {
                    //     let currentD = this.getWorkflowInstanceViewDtos.find((element) => (element.id === viewItem.id));
                    //     currentD.currentStepId =  pointResult[0].id;
                    //     currentD.currentStepTime =  pointResult[0].startTime;
                    //     currentD.receivedStepTimeInterval = this.dateDifference(pointResult[0].startTime);
                    // });

                    if (viewItem.status != 0) {
                        viewItem.currentStepName = '-';
                    }
                    if (!!viewItem.id) {
                        this._outsourcingWorkFlowServiceProxy.getApplicationCodeByWorkFlow(viewItem.id, viewItem.workflowDefinitionId)
                        .subscribe(result => {
                            console.log(result)
                            viewItem.applyCode = result.requisitionCode; viewItem.code = result.code; });
                    } else { viewItem.applyCode = '-'; viewItem.code = '-'; }
                });
            });

    }
    reloadPage(): void {
        this.paginator.changePage(this.paginator.getPage())
    }

    Cancel(workflowid: string) {
        console.log(workflowid);
        this._workflowServiceProxy.cancelInstanceAsync(workflowid)
            .subscribe(resultData => {
                console.log(resultData);
                this.reloadPage();
                this.notify.success('撤销成功');
            });
    }

    workflowstate(state): string {
        // Calculate time between two dates:
        if (state == 0) return '运行';
        if (state == 1) return '已暂停';
        if (state == 2) return '完成';
        if (state == 3) return '已终止';
    }

    Detail(records) {
        console.log(records);
        this.flowworkauditDetailModel.show(records.workflowDefinitionId, records.id);
    }
}
